import * as api from '@/api/packing/index';
import * as baseApi from '@/api/common/index';
export default {
  data() {
    return {
      page: null,
      loading: false,
      applyId: null,
      baseUrl: process.env.BASE_FILE_URL,
      applyGraphicItemsListStatusOptions: [{
        id: 0,
        labelZhCh: '待设计',
        labelEnUs: '待设计'
      }, {
        id: 1,
        labelZhCh: '设计中',
        labelEnUs: '设计中'
      }, {
        id: 2,
        labelZhCh: '可发稿',
        labelEnUs: '可发稿'
      }, {
        id: 3,
        labelZhCh: '已发布',
        labelEnUs: '已发布'
      }, {
        id: 4,
        labelZhCh: '已作废',
        labelEnUs: '已作废'
      }, {
        id: 5,
        labelZhCh: '驳回',
        labelEnUs: '驳回'
      }, {
        id: 6,
        labelZhCh: '审核中',
        labelEnUs: '审核中'
      }],
      packingData: {},
      // erp搜索物料
      materialList: [],
      // 选择物料校验输入项必填
      materailFormRules: {
        materialNo: [{
          required: true
        }],
        materialName: [{
          required: true
        }],
        makingContent: [{
          required: true
        }]
      },
      dialogVisible: false,
      // 彩盒表面处理选项
      colorBoxOptions: [{
        id: 1,
        labelZhCh: 'UV上光',
        labelEnUs: 'UV上光'
      },
      {
        id: 2,
        labelZhCh: '覆亮膜',
        labelEnUs: '覆亮膜'
      },
      {
        id: 3,
        labelZhCh: '覆哑膜',
        labelEnUs: '覆哑膜'
      },
      {
        id: 0,
        labelZhCh: '其他',
        labelEnUs: '其他'
      }
      ],
      // 彩盒接合方式
      colorBoxBindingOptions: [{
        id: 1,
        labelZhCh: '打双钉',
        labelEnUs: '打双钉'
      },
      {
        id: 2,
        labelZhCh: '打单钉',
        labelEnUs: '打单钉'
      },
      {
        id: 3,
        labelZhCh: '胶粘',
        labelEnUs: '胶粘'
      },
      {
        id: 0,
        labelZhCh: '其他',
        labelEnUs: '其他'
      }
      ],
      // 说明书封面/标贴材质选项
      manualMaterialOptions: [{
        id: 1,
        labelZhCh: 'PET',
        labelEnUs: 'PET'
      },
      {
        id: 2,
        labelZhCh: '透明PET',
        labelEnUs: '透明PET'
      },
      {
        id: 3,
        labelZhCh: 'PC磨砂',
        labelEnUs: 'PC磨砂'
      },
      {
        id: 0,
        labelZhCh: '其他',
        labelEnUs: '其他'
      }
      ],
      // 说明书封面/标贴表面材质选项
      manualCoverPageMaterialOptions: [{
        id: 1,
        labelZhCh: '覆亮膜',
        labelEnUs: '覆亮膜'
      },
      {
        id: 2,
        labelZhCh: '覆哑膜',
        labelEnUs: '覆哑膜'
      },
      {
        id: 3,
        labelZhCh: '磨砂膜',
        labelEnUs: '磨砂膜'
      },
      {
        id: 0,
        labelZhCh: '其他',
        labelEnUs: '其他'
      }
      ],
      // 添加料件种类
      materialTypeOptions: [{
        id: 1,
        labelZhCh: '彩盒',
        labelEnUs: '彩盒'
      },
      {
        id: 2,
        labelZhCh: '外箱',
        labelEnUs: '外箱'
      },
      {
        id: 3,
        labelZhCh: '标贴',
        labelEnUs: '标贴'
      },
      {
        id: 4,
        labelZhCh: '说明书',
        labelEnUs: '说明书'
      },
      {
        id: 5,
        labelZhCh: '彩卡',
        labelEnUs: '彩卡'
      },
      {
        id: 6,
        labelZhCh: '其他', // 接口没说明
        labelEnUs: '其他'
      }
      ],
      // 美工设计申请状态
      statusOptions: [{
        id: 0,
        labelZhCh: '待设计', // 设计中
        labelEnUs: '待设计'
      },
      {
        id: 1,
        labelZhCh: '已设计', // 待确认
        labelEnUs: '已设计'
      },
      {
        id: 2,
        labelZhCh: '已确认',
        labelEnUs: '已确认'
      },
      {
        id: 3,
        labelZhCh: '已发布',
        labelEnUs: '已发布'
      },
      {
        id: 4,
        labelZhCh: '已作废',
        labelEnUs: '已作废'
      }
      ],
      basicInfo: [{
        label: '编号',
        unit: '',
        element: 'input',
        name: 'applyNo',
        type: '',
        isRequired: true
      },
      {
        label: '申请人',
        unit: '',
        element: 'input',
        name: 'applyUserName',
        type: '',
        hidden: true,
        options: [],
        isRequired: false
      },
      {
        label: '申请部门',
        unit: '',
        element: 'input',
        options: [],
        name: 'departName',
        hidden: true,
        type: '',
        isRequired: false
      },
      {
        label: '标题',
        unit: '',
        element: 'input',
        name: 'title',
        type: '',
        col: 100,
        isRequired: true
      },
      {
        label: '客户',
        unit: '',
        element: 'selectFilter',
        options: [],
        name: 'customerId',
        type: 'filter',
        isRequired: true
      },
      {
        label: '机型号',
        unit: '',
        element: 'selectFilter',
        name: 'plmModelNo',
        type: '',
        isRequired: true
      },
      {
        label: '出货批次',
        unit: '',
        element: 'input',
        name: 'shippingBatch',
        type: '',
        isRequired: true
      },
      {
        label: '出货时间',
        unit: '',
        element: 'datePicker',
        name: 'shippingDate',
        type: '',
        isRequired: true
      },
      {
        label: '出货数量',
        unit: '',
        element: 'input',
        name: 'shippingQuantity',
        type: 'number',
        isRequired: true
      },
      {
        label: '包装工程师',
        unit: '',
        element: 'departAdmin',
        options: [],
        name: 'packagingStructureEngineer',
        type: '',
        multiple: false,
        isRequired: false
      },
      {
        label: '美工',
        unit: '',
        element: 'departAdmin',
        options: [],
        name: 'packagingGraphicEngineer',
        type: '',
        multiple: false,
        isRequired: false
      },
      {
        label: '出货外箱情况',
        unit: '',
        element: 'input',
        name: 'shippingBoxCase',
        type: '',
        col: 100,
        isRequired: true
      },
      {
        label: '文稿来源',
        unit: '',
        element: 'input',
        name: 'designDocSource',
        type: '',
        col: 100,
        isRequired: true
      },
      {
        label: '备注',
        unit: '',
        element: 'textarea',
        name: 'remark',
        type: '',
        col: 100,
        isRequired: false
      }
      ],
      basicInfoTypeOther: [{
        label: '材质',
        unit: '',
        element: 'select',
        name: 'itemMaterial',
        type: 'number',
        value: 'id',
        options: [],
        isRequired: true
      },
      {
        label: '长',
        unit: '',
        element: 'input',
        name: 'itemLength',
        type: 'number',
        append: 'cm',
        isRequired: true
      },
      {
        label: '宽',
        unit: '',
        element: 'input',
        name: 'itemWidth',
        type: 'number',
        append: 'cm',
        isRequired: true
      },
      {
        label: '规格补充说明',
        unit: '',
        element: 'textarea',
        options: [],
        name: 'remark',
        type: '',
        isRequired: false
      }
      ],
      basicInfoOutBox: [{
        label: '是否有水印',
        unit: '',
        element: 'radio',
        name: 'watermark',
        type: '',
        options: [{
          labelEnUs: 'Y',
          labelZhCh: '是',
          value: 1
        },
        {
          labelEnUs: 'N',
          labelZhCh: '否',
          value: 0
        }
        ],
        isRequired: true
      },
      {
        label: '瓦楞规格',
        unit: '',
        element: 'select',
        options: [],
        name: 'corrugatedSpec',
        type: '',
        isRequired: true
      },
      {
        label: '包材基础料号',
        unit: '',
        element: 'select',
        options: [],
        name: 'corrugatedNo', // corrugatedSpec
        type: '',
        isRequired: true
      },
      {
        label: '长',
        unit: '',
        element: 'input',
        options: [],
        name: 'boxLength',
        append: 'cm',
        type: 'number',
        isRequired: false
      },
      {
        label: '宽',
        unit: '',
        element: 'input',
        options: [],
        name: 'boxWidth',
        type: 'number',
        append: 'cm',
        isRequired: false
      },
      {
        label: '高',
        unit: '',
        element: 'input',
        options: [],
        name: 'boxHeight',
        type: 'number',
        append: 'cm',
        isRequired: false
      },
      {
        label: '专色',
        unit: '',
        element: 'input',
        options: [],
        name: 'specialColorDesc',
        type: '',
        col: 100,
        isRequired: false
      },
      {
        label: '其他属性',
        unit: '',
        element: 'checkbox',
        options: [{
          id: '0',
          value: 0,
          labelZhCh: '接片',
          labelEnUs: '接片'
        },
        {
          id: '1',
          value: 1,
          labelZhCh: '加强手提孔',
          labelEnUs: '加强手提孔'
        },
        {
          id: '2',
          value: 2,
          labelZhCh: '局部UV',
          labelEnUs: '局部UV'
        },
        {
          id: '3',
          value: 3,
          labelZhCh: '防晒200小时',
          labelEnUs: '防晒200小时'
        },
        {
          id: '4',
          value: 4,
          labelZhCh: 'UV',
          labelEnUs: 'UV'
        },
        {
          id: '5',
          value: 5,
          labelZhCh: '胶粘',
          labelEnUs: '胶粘'
        },
        {
          id: '6',
          value: 6,
          labelZhCh: '打钉',
          labelEnUs: '打钉'
        },
        {
          id: '7',
          value: 7,
          labelZhCh: '插舌',
          labelEnUs: '插舌'
        }
        ],
        name: 'boxSpec',
        type: '',
        isRequired: false
      },
      {
        label: '特殊规格',
        unit: '',
        element: 'textarea',
        options: [],
        name: 'boxSpecialSpec',
        type: '',
        isRequired: false
      }],
      basicInfoTypeBox: [{
        label: '是否有水印',
        unit: '',
        element: 'radio',
        name: 'watermark',
        type: '',
        options: [{
          labelEnUs: 'Y',
          labelZhCh: '是',
          value: 1
        },
        {
          labelEnUs: 'N',
          labelZhCh: '否',
          value: 0
        }
        ],
        isRequired: true
      },
      {
        label: '瓦楞规格',
        unit: '',
        element: 'select',
        options: [],
        name: 'corrugatedSpec',
        type: '',
        isRequired: true
      },
      {
        label: '包材基础料号',
        unit: '',
        element: 'select',
        options: [],
        name: 'corrugatedNo', // corrugatedSpec
        type: '',
        isRequired: true
      },
      {
        label: '盒型',
        unit: '',
        element: 'select',
        name: 'colorBoxType',
        col: 100,
        options: [{
          id: 1,
          labelZhCh: '平底平盖',
          value: 1
        },
        {
          id: 2,
          labelZhCh: '异形盒',
          value: 2
        }
        ],
        isRequired: true
      },
      {
        label: '长',
        unit: '',
        element: 'input',
        options: [],
        name: 'boxLength',
        append: 'cm',
        type: 'number',
        hidden: true,
        isRequired: false
      },
      {
        label: '宽',
        unit: '',
        element: 'input',
        options: [],
        name: 'boxWidth',
        type: 'number',
        append: 'cm',
        hidden: true,
        isRequired: false
      },
      {
        label: '高',
        unit: '',
        element: 'input',
        options: [],
        name: 'boxHeight',
        type: 'number',
        append: 'cm',
        hidden: true,
        isRequired: false
      },
      {
        label: '总长',
        unit: '',
        element: 'input',
        options: [],
        hidden: true,
        name: 'boxLengthTotal',
        append: 'cm',
        type: 'number',
        isRequired: false
      },
      {
        label: '总宽',
        unit: '',
        element: 'input',
        options: [],
        hidden: true,
        name: 'boxWidthTotal',
        type: 'number',
        append: 'cm',
        isRequired: false
      },
      {
        label: '专色',
        unit: '',
        element: 'input',
        options: [],
        name: 'specialColorDesc',
        type: '',
        col: 100,
        isRequired: false
      },
      {
        label: '其他属性',
        unit: '',
        element: 'checkbox',
        options: [{
          id: '0',
          value: 0,
          labelZhCh: '接片',
          labelEnUs: '接片'
        },
        {
          id: '1',
          value: 1,
          labelZhCh: '加强手提孔',
          labelEnUs: '加强手提孔'
        },
        {
          id: '2',
          value: 2,
          labelZhCh: '局部UV',
          labelEnUs: '局部UV'
        },
        {
          id: '3',
          value: 3,
          labelZhCh: '防晒200小时',
          labelEnUs: '防晒200小时'
        },
        {
          id: '4',
          value: 4,
          labelZhCh: 'UV',
          labelEnUs: 'UV'
        },
        {
          id: '5',
          value: 5,
          labelZhCh: '胶粘',
          labelEnUs: '胶粘'
        },
        {
          id: '6',
          value: 6,
          labelZhCh: '打钉',
          labelEnUs: '打钉'
        },
        {
          id: '7',
          value: 7,
          labelZhCh: '插舌',
          labelEnUs: '插舌'
        }
        ],
        name: 'boxSpec',
        type: '',
        isRequired: false
      },
      {
        label: '特殊规格',
        unit: '',
        element: 'textarea',
        options: [],
        name: 'boxSpecialSpec',
        type: '',
        isRequired: false
      }],
      typeLable: [{
        label: '颜色',
        unit: '',
        element: 'select',
        name: 'manualColor',
        type: '',
        options: [{
          id: 1,
          labelZhCh: '黑白印刷',
          labelEnUs: '黑白印刷',
          value: 1
        },
        {
          id: 2,
          labelZhCh: '彩色印刷',
          labelEnUs: '彩色印刷',
          value: 2
        }
        ],
        isRequired: true
      },
      {
        label: '尺寸',
        unit: '',
        element: 'select',
        name: 'manualSize',
        type: '',
        options: [{
          id: 1,
          labelZhCh: '148.5*210(mm)',
          labelEnUs: '148.5*210(mm)',
          value: 1
        },
        {
          id: 2,
          labelZhCh: '210*295(mm)',
          labelEnUs: '210*295(mm)',
          value: 2
        }
        ],
        isRequired: true
      },
      {
        label: '流水号',
        unit: '',
        element: 'input',
        name: 'serialNumber',
        type: '',
        isRequired: false
      },
      {
        label: '页数',
        unit: '',
        element: 'input',
        name: 'manualPageSize',
        type: 'number',
        append: '张/A4',
        isRequired: true
      },
      {
        label: '材质',
        value: 'id',
        unit: '',
        element: 'select',
        name: 'manualMaterial',
        type: '',
        options: [],
        isRequired: true
      },
      {
        label: '封面页数',
        unit: '',
        element: 'input',
        name: 'manualCoverPageSize',
        hidden: true,
        type: 'number',
        append: '张/A4',
        isRequired: false
      },
      {
        label: '内页页数',
        unit: '',
        element: 'input',
        name: 'manualInnerPageSize',
        hidden: true,
        type: 'number',
        append: '张/A4',
        isRequired: false
      },
      {
        label: '封面材质',
        unit: '',
        element: 'select',
        name: 'manualCoverPageMaterial',
        hidden: true,
        type: '',
        value: 'id',
        options: [],
        isRequired: false
      },
      {
        label: '内页材质',
        unit: '',
        element: 'select',
        name: 'manualInnerPageMaterial',
        hidden: true,
        options: [],
        type: '',
        value: 'id',
        isRequired: false
      },
      {
        label: '特殊说明',
        unit: '',
        element: 'textarea',
        name: 'remark',
        type: '',
        isRequired: false
      }],
      basicInfoTypeLable: [{
        label: '颜色',
        unit: '',
        element: 'select',
        name: 'manualColor',
        type: '',
        options: [{
          id: 1,
          labelZhCh: '黑白印刷',
          labelEnUs: '黑白印刷',
          value: 1
        },
        {
          id: 2,
          labelZhCh: '彩色印刷',
          labelEnUs: '彩色印刷',
          value: 2
        }
        ],
        isRequired: true
      },
      {
        label: '尺寸',
        unit: '',
        element: 'select',
        name: 'manualSize',
        type: '',
        options: [{
          id: 1,
          labelZhCh: '148.5*210(mm)',
          labelEnUs: '148.5*210(mm)',
          value: 1
        },
        {
          id: 2,
          labelZhCh: '210*295(mm)',
          labelEnUs: '210*295(mm)',
          value: 2
        }
        ],
        isRequired: true
      },
      {
        label: '流水号',
        unit: '',
        element: 'input',
        name: 'serialNumber',
        type: '',
        isRequired: false
      },
      {
        label: '页数',
        unit: '',
        element: 'input',
        name: 'manualPageSize',
        type: 'number',
        append: '张/A4',
        isRequired: true
      },
      {
        label: '材质',
        value: 'id',
        unit: '',
        element: 'select',
        name: 'manualMaterial',
        type: '',
        options: [],
        isRequired: true
      },
      {
        label: '封面页数',
        unit: '',
        element: 'input',
        name: 'manualCoverPageSize',
        hidden: true,
        type: 'number',
        append: '张/A4',
        isRequired: false
      },
      {
        label: '内页页数',
        unit: '',
        element: 'input',
        name: 'manualInnerPageSize',
        hidden: true,
        type: 'number',
        append: '张/A4',
        isRequired: false
      },
      {
        label: '封面材质',
        unit: '',
        element: 'select',
        name: 'manualCoverPageMaterial',
        hidden: true,
        type: '',
        options: [],
        isRequired: false
      },
      {
        label: '内页材质',
        unit: '',
        element: 'select',
        name: 'manualInnerPageMaterial',
        hidden: true,
        options: [],
        type: '',
        isRequired: false
      },
      {
        label: '特殊说明',
        unit: '',
        element: 'textarea',
        name: 'remark',
        type: '',
        isRequired: false
      }],
      basicInfoTypeOutBox: [],
      basicInfoTypeDes: [],
      basicInfoTypeColorCard: []
    };
  },
  created() {
    this.dataInit()
  },
  computed: {
    getValue() {
      return (list, keyName, valueName, value) => {
        let resStr = '';
        if (list) {
          list.map(item => {
            if (item[keyName] == value) {
              resStr = item[valueName];
            }
          })
        }
        return resStr;
      }
    }
  },
  methods: {
    dataInit() {
      const data = {
        // 基本信息
        applyGraphic: {},
        applyGraphicItemsList: []
      }
      this.packingData = JSON.parse(JSON.stringify(data))
    },
    // 显示基本信息初始化
    setInitBasicFormShow() {
      this.basicInfo.map(item => {
        item.hidden = false;
        if (
          item.name === 'packagingStructureEngineer' ||
          item.name === 'packagingGraphicEngineer'
        ) {
          item.name = item.name + 'Name';
          item.element = 'input';
        }
        if (item.name === 'customerId') {
          item.name = 'customer' + 'Name';
          item.element = 'input';
        }
        if (item.name === 'plmModelNo') {
          item.element = 'input';
        }
      });
    },
    // 包材选择物料
    getMaterialByNo(val) {
      api.getPdsPackingErpMaterialByNo({ materialNo: val }).then(res => {
        this.materialList = res.data;
      });
    },
    // 获取材质下拉列表并渲染到各个表单上
    getMaterialOptions() {
      const basicForm = [
        'basicInfoTypeOutBox',
        'basicInfoTypeLable',
        'basicInfoTypeDes',
        'basicInfoTypeColorCard',
        'basicInfoTypeOther'
      ];
      const materialOptionsList = [
        'outerBoxList',
        'labelList',
        'manualList',
        'cardList',
        'otherList'
      ];
      api.getMaterial(null).then(res => {
        basicForm.map((item, index) => {
          if (item === 'basicInfoTypeDes') {
            this[item] = JSON.parse(JSON.stringify(this.typeLable));
          }
          if (item === 'basicInfoTypeOutBox') {
            this[item] = JSON.parse(JSON.stringify(this.basicInfoTypeOther));
          }
          if (item === 'basicInfoTypeLable') {
            this[item] = JSON.parse(JSON.stringify(this.basicInfoTypeOther));
          }
          if (item === 'basicInfoTypeColorCard') {
            this[item] = JSON.parse(JSON.stringify(this.basicInfoTypeOther));
          }
          if (item !== 'basicInfoTypeOther' && item !== 'basicInfoTypeDes') {
            this[item] = JSON.parse(JSON.stringify(this.basicInfoTypeOther));
          }
          // 只有说明书有封面+内页选项
          if (item === 'basicInfoTypeDes') {
            this[item][0].options.push(
              {
                id: 3,
                labelZhCh: '封面彩色+内页黑白',
                value: 3
              })
          }
          const options = res.data[materialOptionsList[index]];
          options.map(option => {
            option.labelZhCh = option.materialName;
          });
          const basicFormInfo = this[item];

          basicFormInfo.map(element => {
            if (
              element.name === 'manualMaterial' ||
              element.name === 'itemMaterial' || element.name === 'manualCoverPageMaterial' || element.name === 'manualInnerPageMaterial'
            ) {
              element.options = options;
            }
          });
        });
      });
    },
    // 获取字典选项
    getDict() {
      const codes = 'pds_packing_corrugat';
      baseApi.getTypesValue(codes).then(res => {
        this.$set(
          this.basicInfoTypeBox[1],
          'options',
          res.data.pds_packing_corrugat
        );
        this.$set(
          this.basicInfoOutBox[1],
          'options',
          res.data.pds_packing_corrugat
        );
        
      });
    },

    /* 获取包装工程师列表 */
    getDeptPersonsList(id, index) {
      baseApi.getUserByIds(id).then(res => {
        if (res.code == 200) {
          this.basicInfo[index].options = res.data;
        }
      });
    },
    // 获取详情
    getApplyGraphicById() {
      if (!this.applyId) {
        return
      }
      this.loading = true;
      api.getApplyGraphicById({ id: this.applyId }).then(res => {
        this.refreshCb(res);
      });
    },
    refreshCb(res) {
      this.loading = false;
      if (res.code === 200) {
        res.data.applyGraphic = res.data.applyGraphic || {}
        if (res.data.applyGraphic.packagingStructureEngineer) {
          this.getDeptPersonsList(
            res.data.applyGraphic.packagingStructureEngineer,
            9
          );
        }
        if (res.data.applyGraphic.packagingGraphicEngineer) {
          this.getDeptPersonsList(
            res.data.applyGraphic.packagingGraphicEngineer,
            10
          );
        }
        // 客户回显处理
        if (res.data.applyGraphic.customerId && this.$refs.basicForm) {
          this.$refs.basicForm.getPdsCustomerByName(
            res.data.applyGraphic.customerName
          );
        }

        res.data.applyGraphicItemsList = res.data.applyGraphicItemsList || [];
        res.data.applyGraphicItemsList.map(item => {
          item.checked = false
          const expandArr = ['pdsPackingDesignGraphicBox', 'pdsPackingDesignGraphicManual', 'pdsPackingDesignGraphicOther']
          expandArr.map(objName => {
            item[objName] = item[objName] || {}
          })
          item.fileId = item.fileId || null
          item.fileName = item.fileName || null
          item.fileUrl = item.fileUrl || null
          if (item.pdsPackingDesignGraphicBox) {
            item.pdsPackingDesignGraphicBox.boxSpec = item
              .pdsPackingDesignGraphicBox.boxSpec
              ? item.pdsPackingDesignGraphicBox.boxSpec.split(',')
              : [];
          }
        });
        this.packingData = res.data;
      }
      if (res.code === 200 && this.$route.query.type === 'export' && !this.modelPbsId) {
        this.exportLoading = false
        setTimeout(() => {
          this.setPdfPage();
        })
      }
    }

  }
}
